package com.qdxwx.manage.support.factory;

import com.qdxwx.common.utils.*;
import com.qdxwx.data.service.manager.ManagerLoginServiceImpl;
import com.qdxwx.data.service.manager.ManagerOnlineService;
import com.qdxwx.data.service.operLog.OperLogService;
import com.qdxwx.manage.config.LoginConstants;
import com.qdxwx.models.manager.ManagerLogin;
import com.qdxwx.models.manager.ManagerOnline;
import com.qdxwx.models.operLog.OperLog;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TimerTask;


/**
 * 异步工厂（产生任务用）
 *
 * @author liuhulu
 *
 */
public class AsyncFactory {
    private static final Logger logger = LoggerFactory.getLogger(AsyncFactory.class);

    /**
     * 同步session到数据库
     *
     * @return 任务task
     */
    public static TimerTask syncSessionToDb(final ManagerOnline online) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(ManagerOnlineService.class).save(online);
            }
        };
    }

    /**
     * 操作日志记录
     *
     * @param operLog 操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOper(final OperLog operLog) {
        return new TimerTask() {
            @Override
            public void run() {
                // 远程查询操作地点
                operLog.setOperLocation(IPAddressUtils.getRealAddressByIP(operLog.getOperIp()));
                SpringUtils.getBean(OperLogService.class).insert(operLog);
            }
        };
    }

    /**
     * 记录登陆信息
     *
     * @param username 用户名
     * @param status   状态
     * @param message  消息
     * @param args     列表
     * @return 任务task
     */
    public static TimerTask recordManagerLogin(final String username, final String status, final String message, final Object... args) {
        HttpServletRequest request = TKServlet.getRequest();
        String userAgent = TKRequest.getUserAgent(request);
        String ip = TKRequest.getIpAddr(request);
        return new TimerTask() {
            @Override
            public void run() {
                String address = IPAddressUtils.getRealAddressByIP(ip);
                String s = '[' + ip + ']' +
                        '[' + address + ']' +
                        '[' + username + ']' +
                        '[' + status + ']' +
                        '[' + message + ']';
                // 打印信息到日志
                logger.info(s, args);
                // 获取客户端操作系统
                String os = TKClientOs.getName(userAgent);
                // 获取客户端浏览器
                String browser = TKClientBrowser.getTitle(userAgent);
                // 封装对象
                ManagerLogin managerLogin = new ManagerLogin();
                managerLogin.setLoginName(username);
                managerLogin.setIpAddr(ip);
                managerLogin.setLoginLocation(address);
                managerLogin.setBrowser(browser);
                managerLogin.setOs(os);
                managerLogin.setMsg(message);
                // 日志状态
                if (StringUtils.equalsAny(status, LoginConstants.LOGIN_SUCCESS, LoginConstants.LOGOUT)) {
                    managerLogin.setStatus(0);
                } else if (LoginConstants.LOGIN_FAIL.equalsIgnoreCase(status)) {
                    managerLogin.setStatus(1);
                }
                // 插入数据
                SpringUtils.getBean(ManagerLoginServiceImpl.class).insert(managerLogin);
            }
        };
    }
}
